package com.ruoyi.system.service.impl;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SetLine;
import com.ruoyi.system.domain.SetStation;
import com.ruoyi.system.domain.SetStationLine;
import com.ruoyi.system.mapper.SetLineMapper;
import com.ruoyi.system.mapper.SetStationLineMapper;
import com.ruoyi.system.mapper.SetStationMapper;
import com.ruoyi.system.service.ISetStationLineService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 工位-线体Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-11-21
 */
@Service
public class SetStationLineServiceImpl implements ISetStationLineService
{
    private static final Logger log = LoggerFactory.getLogger(SetStationLineServiceImpl.class);

    @Autowired
    private SetStationLineMapper setStationLineMapper;

    @Autowired
    private SetLineMapper setLineMapper;

    @Autowired
    private SetStationMapper setStationMapper;

    /**
     * 查询工位-线体
     * 
     * @param id 工位-线体主键
     * @return 工位-线体
     */
    @Override
    public SetStationLine selectSetStationLineById(Long id)
    {
        return setStationLineMapper.selectSetStationLineById(id);
    }

    /**
     * 查询工位-线体-线体
     *
     * @param stationCode 工位-线体编码
     * @param lineCode 线体编码
     * @return 工位-线体-线体
     */
    public SetStationLine selectSetStationLineByCode(String stationCode,String lineCode)
    {
        return setStationLineMapper.selectSetStationLineByCode(stationCode,lineCode);
    }

    /**
     * 查询工位-线体列表
     * 
     * @param setStationLine 工位-线体
     * @return 工位-线体
     */
    @Override
    public List<SetStationLine> selectSetStationLineList(SetStationLine setStationLine)
    {
        return setStationLineMapper.selectSetStationLineList(setStationLine);
    }

    /**
     * 新增工位-线体
     * 
     * @param setStationLine 工位-线体
     * @return 结果
     */
    @Override
    @Transactional
    public int insertSetStationLine(SetStationLine setStationLine)
    {
        setStationLine.setCreateTime(DateUtils.getNowDate());
        setStationLine.setCreateBy(SecurityUtils.getUsername());
        setStationLine.setStatus(UserConstants.NORMAL);
        return setStationLineMapper.insertSetStationLine(setStationLine);
    }

    /**
     * 修改工位-线体
     * 
     * @param setStationLine 工位-线体
     * @return 结果
     */
    @Override
    @Transactional
    public int updateSetStationLine(SetStationLine setStationLine)
    {
        setStationLine.setUpdateTime(DateUtils.getNowDate());
        setStationLine.setUpdateBy(SecurityUtils.getUsername());
        return setStationLineMapper.updateSetStationLine(setStationLine);
    }

    /**
     * 批量删除工位-线体
     *
     * @param ids 需要删除的工位-线体主键
     * @return 结果
     */
    @Override
    public int deleteSetStationLineByIds(Long[] ids) {
        return setStationLineMapper.deleteSetStationLineByIds(ids);
    }

    /**
     * 删除工位-线体信息
     * 
     * @param id 工位-线体主键
     * @return 结果
     */
    @Override
    public int deleteSetStationLineById(Long id)
    {
        return setStationLineMapper.deleteSetStationLineById(id);
    }

    /**
     * 导入工位-线体数据
     *
     * @param stationLineList 工位-线体数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importStationLine(List<SetStationLine> stationLineList, boolean isUpdateSupport, String operName) {

        if (StringUtils.isNull(stationLineList) || stationLineList.size() == 0)
        {
            throw new CustomException("导入工位-线体数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        //String password = configService.selectConfigByKey("sys.user.initPassword");
        for (SetStationLine setStationLine : stationLineList)
        {
            try
            {
                // 验证是否存在这个工位-线体
                SetStationLine ssl = setStationLineMapper.selectSetStationLineByCode(setStationLine.getStationCode(),setStationLine.getLineCode());

                if (StringUtils.isNull(ssl))
                {
                    SetLine sl = setLineMapper.selectSetLineByLineCode(setStationLine.getLineCode());
                    SetStation ss = setStationMapper.selectSetStationByCode(setStationLine.getStationCode());
                    if(StringUtils.isNull(ss) || StringUtils.isNull(sl)){
                        failureNum++;
                        failureMsg.append("<br/>" + failureNum + "、工位编码-产线编码： " + ((ss == null)?null:ss.getStationCode()) + "-" + ((sl == null)?null:sl.getLineCode()) + " 在数据库中不存在");
                    }else{
                        setStationLine.setCreateBy(operName);
                        this.insertSetStationLine(setStationLine);
                        successNum++;
                        successMsg.append("<br/>" + successNum + "、工位编码-产线编码： " + setStationLine.getStationCode() + "-" + setStationLine.getLineCode() + " 导入成功");
                    }

                }
                else if (isUpdateSupport)
                {
                    setStationLine.setUpdateBy(operName);
                    this.updateSetStationLine(setStationLine);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、工位编码-产线编码： " + setStationLine.getStationCode() + "-" + setStationLine.getLineCode() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、工位编码-产线编码： " + setStationLine.getStationCode() + "-" + setStationLine.getLineCode() + " 已存在");
                }

            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、工位编码-产线编码： " + setStationLine.getStationCode() + "-" + setStationLine.getLineCode() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
